﻿/**
 *
 * @title: Basic MVC Framework, View Class
 * @author: 
 * @license: 
 * @updated: 29/07/2010
 *
 * @version	1.0 
 */

package com.mediagallary.view {

	import com.mediagallary.model.*;
	import com.mediagallary.controller.*;
	import com.mediagallary.view.*;
	import com.mediagallary.utils.*;
	//import com.mediagallary.events.CustomEvent;
	
	import gs.TweenLite;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.net.URLRequest;

	import flash.filters.DropShadowFilter;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.Sprite;
	
	/**
	 * Basic View class in Model/View/Controller triad.
	 */
	public class GallaryView extends View {
		
		private var _model:Model;
		private var _controller:Controller;
		
		private var loader:Loader;
		private var bmp:Bitmap;
		private var container:Sprite;
		public var buttonsMc:MovieClip;
		
		// store a reference to the _controller:
		//private var _controller:GallaryController;
		
		private var stageWidth:Number = 550;
		private var stageHeight:Number = 400;

		// we will only allow the user to getNext/Prev image if we 
		// are not currentlyanimating a transition between 2 images:
		private var isAnimating:Boolean;
		
		private var baseURL:String = "./";
		
		public function GallaryView(m:Model, c:Controller, container:Sprite) {
			super(m, c)
			
			this.container = container;
			//addChild(container);
		} 
		
		override public function update(event:Event = null):void
		{
			//trace("IMAGE URL : " + baseURL + model.currentFile );
			for(var i:int = 0; i<model.totalItems; i++){
				var path:String = baseURL + model.gallaryData.item[i].thumb;
				//trace("GallaryView update i : "+i+", path : "+path);
					var thumbLoader:ImageLoader = new ImageLoader();
					thumbLoader.loadImage(path);
					thumbLoader.x = randomNumber(20, stageWidth-200);
					thumbLoader.y = randomNumber(20, stageHeight-100);
					thumbLoader.rotation = randomNumber(-30, 30);
					container.addChild(thumbLoader);
					thumbLoader.addEventListener(MouseEvent.CLICK, thunbClickHandler);
					applyEffect(thumbLoader);
			}
		}
		
		private function thunbClickHandler(event:MouseEvent):void{
			//trace("thunbClickHandler : "+event.currentTarget);
			var target:DisplayObject = event.currentTarget as DisplayObject;
			container.setChildIndex(target, container.numChildren -1);
			controller.currectMediaItem(event);
		}
		
		private function applyEffect(target:ImageLoader):void{
			var myDropShadowFilter:DropShadowFilter = new DropShadowFilter (12,45,0x000000,0.5,10,10,1,2,false,false,false);
			target.filters = [myDropShadowFilter];
			TweenLite.to(target,0.7,{alpha:1,onStart:onTweenStart, onComplete:onTweenComplete});
			/*TweenLite.to(bmp, 0.7, {alpha:0});*/
		}
		
		public function getIsAnimating():Boolean
		{
			return isAnimating;
		}
		
		// set a flag that will allow us to ignore user input while 
		// an animation is in progress
		private function onTweenStart():void
		{
			isAnimating = true;
		}
		
		private function onTweenComplete():void
		{
			isAnimating = false;
		}
		
		
		
		private function loadMedia(event:MouseEvent):void{
			trace("thunbClickHandler : "+event.currentTarget.rotation);
			//controller.getCurrentItem(event);
		}
		
		/** 
		* Generates a truly "random" number
		* @return Random Number
		*/ 
		private function randomNumber(low:Number=0, high:Number=1):Number
		{
			return Math.floor(Math.random() * (1+high-low)) + low;
		}

		
		// show/hide the play & pause buttons to reflect the action currently avalible to the user:
		// the two buttons are stacked on the stage
		public function showButton(b:String):void
		{  
			if(b == "play")
			{
				buttonsMc.play_btn.visible = true;
				buttonsMc.pause_btn.visible = false;
			}
			else
			{
				buttonsMc.play_btn.visible = false;
				buttonsMc.pause_btn.visible = true;
			}	
		}
		
		// pass in a reference to the controller so that it can call getIsAnimating:
		public function setControllerReference(c:GallaryController):void
		{
			_controller = c;
		}
		
		
	}
}